package com.dxq.test.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.common.Constant;
import com.dxq.test.R;
import com.dxq.test.adapter.RemindAdapter;
import com.dxq.test.bean.RemindEntity;
import com.dxq.test.common.DBOperate;
import com.dxq.test.db.Breed;
import com.dxq.test.db.Group;
import com.dxq.test.db.Label;
import com.dxq.test.db.LabelBreed;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.db.Vaccine;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/12/6
 * Describe: 提醒列表页面
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public class RemindActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle, tvSet;
    private RecyclerView recyclerView;

    private RemindAdapter mAdapter;
    private List<RemindEntity> recordList = new ArrayList<>();

    private String[] types = {"牛", "羊", "禽类"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_breed);
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvSet = findViewById(R.id.tvSet);
        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("提醒管理");

        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new RemindAdapter(recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);//添加空布局

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                if (recordList.get(position).getItemType() == 0) {
                    Intent intent = new Intent(RemindActivity.this, BreedAddActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("breed", recordList.get(position).getBreed());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(RemindActivity.this, VaccineAddActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("vaccine", recordList.get(position).getVaccine());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");

            }
        });
    }

    private void getData() {
        recordList.clear();
//        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
//        if (null != pasture) {
//            ForeignCollection<Breed> breeds = pasture.getBreeds();
//            if (!ObjectUtils.isEmpty(breeds)) {
//                for (Breed breed : breeds) {
//                    //创建StringBuffer对象
//                    StringBuffer info_b = new StringBuffer("请记得为");//预产
//                    StringBuffer info_n = new StringBuffer("请记得为");//配种
//                    List<Label> labels_w = tablesDao.queryLabelForBreed(breed, LabelBreed.LABEL_ID_W_NAME);
//                    if (!ObjectUtils.isEmpty(labels_w)) {
//                        breed.setLabel_w(labels_w.get(0));
//                        info_b.append(Constant.SEX_WOMAN + types[App.select_label_type] + "(编号：" + labels_w.get(0).getName() + ")");
//                        info_n.append(Constant.SEX_WOMAN + types[App.select_label_type] + "(编号：" + labels_w.get(0).getName() + ")");
//                    }
//                    List<Label> labels_m = tablesDao.queryLabelForBreed(breed, LabelBreed.LABEL_ID_M_NAME);
//                    if (!ObjectUtils.isEmpty(labels_m)) {
//                        breed.setLabel_m(labels_m.get(0));
//                        info_n.append("与").append(Constant.SEX_MAN + types[App.select_label_type] + "(编号：" + labels_m.get(0).getName() + ")");
//                    }
//                    if (breed.isBirth_time_remind()) {//预产
//                        recordList.add(new RemindEntity(RemindEntity.TYPE_B, info_b.append("进行接生哦！").toString(),
//                                "预产", TimeUtils.string2Millis(breed.getBirth_time()), breed));
//                    }
//                    if (breed.isNext_time_remind()) {//配种
//                        recordList.add(new RemindEntity(RemindEntity.TYPE_B, info_n.append("进行二次配种哦！").toString(),
//                                "配种", TimeUtils.string2Millis(breed.getNext_time()), breed));
//                    }
//                }
//
//            }
//            ForeignCollection<Vaccine> vaccines = pasture.getVaccines();
//            if (null != vaccines && vaccines.size() > 0) {
//                for (Vaccine vaccine : vaccines) {
//                    //创建StringBuffer对象
//                    StringBuffer info = new StringBuffer("请记得为");
//                    //查询防治个体
//                    List<Label> labels = tablesDao.queryLabelForVaccine(vaccine);
//                    if (!ObjectUtils.isEmpty(labels)) {
//                        vaccine.setLabel(labels.get(0));
//                        info.append("(编号：" + labels.get(0).getName() + ")牲畜进行二次'" + vaccine.getType() + "'防治哦！");
//                        if (vaccine.isNext_time_remind())
//                            recordList.add(new RemindEntity(RemindEntity.TYPE_V, info.toString(),
//                                    "防治", TimeUtils.string2Millis(vaccine.getNext_time()), vaccine));
//                    }
//                    //查询防治分群
//                    List<Group> groups = tablesDao.queryGroupForVaccine(vaccine);
//                    if (!ObjectUtils.isEmpty(groups)) {
//                        vaccine.setGroup(groups.get(0));
//                        info.append("'" + groups.get(0).getName() + "'进行二次'" + vaccine.getType() + "'防治哦！");
//                        if (vaccine.isNext_time_remind())
//                            recordList.add(new RemindEntity(RemindEntity.TYPE_V, info.toString(),
//                                    "防治", TimeUtils.string2Millis(vaccine.getNext_time()), vaccine));
//                    }
//                }
//            }
//        }
        recordList = DBOperate.getRemindList(tablesDao);
        //根据时间间隔排序
        Collections.sort(recordList);
        mAdapter.setList(recordList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ivBack:
                RemindActivity.this.finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
